package com.example.hzq.food.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.hzq.food.R;
import com.example.hzq.food.adapter.ShopScoreadapter;
import com.example.hzq.food.bean.FoodBean;
import com.example.hzq.food.bean.OrderBean;
import com.example.hzq.food.model.Model;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.adapter.Shopadapter.shop_id;

/**
 * Created by hzq on 2017/6/15.
 */

public class ShopScorefragment extends Fragment {
    private Spinner spinner;
    private RecyclerView recyclerView;
    private  RecyclerView.LayoutManager layoutManager;
    private Context context;
    private View view;
    private List<OrderBean>list=null;
    private ShopScoreadapter shopScoreadapter;
    private String[] foods;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        view=inflater.inflate(R.layout.shopscorefragment,container,false);
        context=getActivity();
        init();
        spinner=(Spinner)view.findViewById(R.id.spinner1);
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview4);
        layoutManager=new LinearLayoutManager(context);
        shopScoreadapter=new ShopScoreadapter(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(shopScoreadapter);
        return view;
    }
   private void init(){
       final Model model=new Model();
       Call<List<FoodBean>> call=model.getfood(shop_id);
       call.enqueue(new Callback<List<FoodBean>>() {
           @Override
           public void onResponse(Call<List<FoodBean>> call, final Response<List<FoodBean>> response) {
               foods=new String[response.body().size()];
               for (int i=0;i<response.body().size();i++)
                   foods[i]=response.body().get(i).getFoodname();
               ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,foods);
               adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
               spinner.setAdapter(adapter);
               spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
                   @Override
                   public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                       TextView tv = (TextView)view;
                       tv.setGravity(android.view.Gravity.CENTER_HORIZONTAL);
                       Model model1=new Model();
                       Call<List<OrderBean>>call1=model1.getallorder(response.body().get(position).getFood_id());
                       call1.enqueue(new Callback<List<OrderBean>>() {
                           @Override
                           public void onResponse(Call<List<OrderBean>> call, Response<List<OrderBean>> response1) {
                               if(response1.isSuccessful()&&response1.body()!=null)
                               {
                                   list=response1.body();
                                  shopScoreadapter.setList(list);
                               }
                           }

                           @Override
                           public void onFailure(Call<List<OrderBean>> call, Throwable t) {

                           }
                       });

                   }

                   @Override
                   public void onNothingSelected(AdapterView<?> parent) {

                   }
               });
           }

           @Override
           public void onFailure(Call<List<FoodBean>> call, Throwable t) {

           }
       });
   }


}
